import { checkType, randomNum } from "../share/utils";
const reg_onClick = /onClick\=\"(.+?)\"/g;
const reg_fnName = /^(.+?)\(/;
const reg_arg = /\((.*?)\)/;

const eventPool = [];

/**
 * {
 *    mark: random,
 *    handle: 事件处理函数的字符串
 *    type: 'click' // 事件处理的类型
 * }
 */
export function eventFormat(template) {
  return template.replace(reg_onClick, (node, key) => {
    const _mark = randomNum();
    eventPool.push({
      mark: _mark,
      handle: key.trim(),
      type: "click",
    });
    return `data-mark=${_mark}`;
  });
}

export function bindEvent(methods) {
  const allElements = document.querySelectorAll("*");
  let oItem = null;
  let _mark = 0;
  eventPool.forEach((event) => {
    for (let i = 0; i < allElements.length; i++) {
      oItem = allElements[i];
      _mark = parseInt(oItem.dataset.mark);

      if (event.mark === _mark) {
        oItem.addEventListener(
          event.type,
          () => {
            const fnName = event.handle.match(reg_fnName)[1];
            const arg = checkType(event.handle.match(reg_arg)[1]);
            methods[fnName](arg);
          },
          false
        );
      }
    }
  });
}
